﻿Imports Telerik.Web.UI

Partial Class Accounts
    Inherits System.Web.UI.Page

    Dim _CommandName As String
    
    Dim AccountObj As New AccountsSP
    Dim AccountDatatset As New Data.DataSet

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            Dim _lblPageTitle As Label
            _lblPageTitle = Me.Master.FindControl("lblPageTitle")
            _lblPageTitle.Text = GetLocalResourceObject("PageTitle")
        End If

        If Not (Request.Cookies("Language") Is Nothing) AndAlso Not (Request.Cookies("Language").Value Is Nothing) Then
            If (Request.Cookies("Language").Value.ToString().Contains("En") = False) Then
                TDAlignTree.Style(HtmlTextWriterStyle.TextAlign) = "right"
                TDAlign1.Style(HtmlTextWriterStyle.TextAlign) = "right"
                TDAlign2.Style(HtmlTextWriterStyle.TextAlign) = "right"
            ElseIf (Request.Cookies("Language").Value.ToString().Contains("En") = True) Then
                TDAlignTree.Style(HtmlTextWriterStyle.TextAlign) = "left"
                TDAlign1.Style(HtmlTextWriterStyle.TextAlign) = "left"
                TDAlign2.Style(HtmlTextWriterStyle.TextAlign) = "left"
            End If
        End If


        If nameEn.Text = "" And nameAr.Text = "" And Number.Text = "" Then
            btnEdit.Enabled = False
        Else
            btnEdit.Enabled = True
        End If

    End Sub

    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Me.Theme = GetGlobalResourceObject("SystemResource", "Theme")
    End Sub

    Protected Sub rtvAccounts_NodeClick(ByVal sender As Object, ByVal e As RadTreeNodeEventArgs)

        Dim NodeID As String = e.Node.Value.ToString()

        Dim AccountObj As New AccountsSP
        Dim AccountDatatset As New Data.DataSet
        AccountDatatset = AccountObj.QueryDatabase(DBParentStoredProcedureClass.SqlQueryType.FillDataset, "GetAccount", NodeID, , , , , )

        Dim AccountNameEn As String = AccountDatatset.Tables(0).Rows(0).Item("AccountNameEn").ToString()
        Dim AccountNameAr As String = AccountDatatset.Tables(0).Rows(0).Item("AccountNameAr").ToString()
        Dim AccountNumber As String = AccountDatatset.Tables(0).Rows(0).Item("AccountNo").ToString()
        Dim AccountRemarks As String = AccountDatatset.Tables(0).Rows(0).Item("Remarks").ToString()
        Dim AccType As String = AccountDatatset.Tables(0).Rows(0).Item("AccountType").ToString()

        Dim LookupObj As New DBlookup_tableSP
        Dim LookupDatatset As New Data.DataSet
        LookupDatatset = LookupObj.QueryDatabase(DBParentStoredProcedureClass.SqlQueryType.FillDataset, "RtvSelectedLook_up", "15", AccType, , , )

        Dim AccountType As String
        If (Request.Cookies("Language").Value.ToString().Contains("En") = True) Then
            If LookupDatatset.Tables.Count > 0 Then
                If LookupDatatset.Tables(0).Rows.Count > 0 Then
                    AccountType = LookupDatatset.Tables(0).Rows(0).Item("DescriptionEn").ToString()
                Else
                    AccountType = ""
                End If
            End If
        Else
            If LookupDatatset.Tables.Count > 0 Then
                If LookupDatatset.Tables(0).Rows.Count > 0 Then
                    AccountType = LookupDatatset.Tables(0).Rows(0).Item("DescriptionAr").ToString()
                Else
                    AccountType = ""
                End If
            End If
        End If

        pnlrtvAccounts.Visible = True

        nameEn.Text = AccountNameEn
        nameAr.Text = AccountNameAr
        Number.Text = AccountNumber
        Remarks.Text = AccountRemarks
        lblParentAcc.Text = NodeID
        Type.Text = AccountType

        rtbAccountNameEn.Text = AccountNameEn
        rtbAccountNameAr.Text = AccountNameAr
        rtbAccountNumber.Text = AccountNumber
        rtbAccountRemarks.Text = AccountRemarks
        rcbAccountType.DataBind()
        rcbAccountType.SelectedValue = AccType

        btnEdit.Enabled = True

        lblNodeID.Text = NodeID

    End Sub





    Protected Sub btnAddRoot_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAddRoot.Click

        pnlrtvAccounts.Visible = True

        lblParentAcc.Visible = False
        lblParent.Visible = False
        nameEn.Visible = False
        nameAr.Visible = False
        Number.Visible = False
        Remarks.Visible = False
        Type.Visible = False

        rtbAccountNameEn.Visible = True
        rtbAccountNameEn.Text = ""
        rtbAccountNameAr.Visible = True
        rtbAccountNameAr.Text = ""
        rtbAccountNumber.Visible = True
        rtbAccountNumber.Text = ""
        rtbAccountRemarks.Visible = True
        rtbAccountRemarks.Text = ""
        rcbAccountType.Visible = True
        rcbAccountType.DataBind()
        rcbAccountType.SelectedIndex = 0

        btnEdit.Visible = False
        btnSave.Visible = True
        btnCancel.Visible = True

        lblNodeID.Text = ""

        Session("Operation") = "AddRoot"

    End Sub




    Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAdd.Click

        Dim Selected As Boolean = False

        For Each node As Telerik.Web.UI.RadTreeNode In rtvAccounts.GetAllNodes()
            If node.Selected = True Then
                Selected = True
                Exit For
            End If
        Next

        If Selected = False Then
            RadAjaxManager1.ResponseScripts.Add("radalert('Please Select an Account First !', 270, 100);")
            Exit Sub
        End If

        pnlrtvAccounts.Visible = True

        nameEn.Visible = False
        nameAr.Visible = False
        Number.Visible = False
        Remarks.Visible = False
        Type.Visible = False

        lblParentAcc.Visible = True
        lblParent.Visible = True

        rtbAccountNameEn.Visible = True
        rtbAccountNameEn.Text = ""
        rtbAccountNameAr.Visible = True
        rtbAccountNameAr.Text = ""
        rtbAccountNumber.Visible = True
        rtbAccountNumber.Text = ""
        rtbAccountRemarks.Visible = True
        rtbAccountRemarks.Text = ""
        rcbAccountType.Visible = True
        rcbAccountType.DataBind()
        rcbAccountType.SelectedIndex = 0

        btnEdit.Visible = False
        btnSave.Visible = True
        btnCancel.Visible = True

        For Each node As Telerik.Web.UI.RadTreeNode In rtvAccounts.GetAllNodes()
            If node.Selected = True Then
                lblNodeID.Text = node.Value.ToString()
                Exit For
            End If
        Next

        Session("Operation") = "AddNew"

    End Sub




    Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnDelete.Click

        'Dim x As Integer = 0

        Dim Selected As Boolean = False

        For Each node As Telerik.Web.UI.RadTreeNode In rtvAccounts.GetAllNodes()
            If node.Selected = True Then
                Selected = True
                Dim ID As String = node.Value
                AccountDatatset = AccountObj.QueryDatabase(DBParentStoredProcedureClass.SqlQueryType.FillDataset, "RtvChildAccounts", , , , ID, )
                If AccountDatatset.Tables(0).Rows.Count > 0 Then
                    RadAjaxManager1.ResponseScripts.Add("radalert('You have to delete any Child Item associated, Then You can proceed with deletion !', 270, 100);")
                    Exit Sub
                End If
                Exit For
            End If
        Next

        If Selected = False Then
            RadAjaxManager1.ResponseScripts.Add("radalert('Please Select an Account First !', 270, 100);")
            Exit Sub
        End If

        For Each node As Telerik.Web.UI.RadTreeNode In rtvAccounts.GetAllNodes()
            If node.Selected = True Then
                Session("Operation") = "Delete"
                lblNodeID.Text = node.Value.ToString()
                UpdateAccounts(Session("Operation").ToString())
                rtvAccounts.DataBind()
                Exit For
            End If
        Next
    End Sub

    Protected Sub btnEdit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnEdit.Click

        Dim Selected As Boolean = False

        For Each node As Telerik.Web.UI.RadTreeNode In rtvAccounts.GetAllNodes()
            If node.Selected = True Then
                Selected = True
                Exit For
            End If
        Next

        If Selected = False Then
            RadAjaxManager1.ResponseScripts.Add("radalert('Please Select an Account First !', 270, 100);")
            Exit Sub
        End If

        '''''' Show and Hide Controls ''''''

        nameEn.Visible = False
        nameAr.Visible = False
        Number.Visible = False
        Remarks.Visible = False
        Type.Visible = False

        rtbAccountNameEn.Visible = True
        rtbAccountNameAr.Visible = True
        rtbAccountNumber.Visible = True
        rtbAccountRemarks.Visible = True
        rcbAccountType.Visible = True

        btnEdit.Visible = False
        btnSave.Visible = True
        btnCancel.Visible = True

        '''''' Show and Hide Controls ''''''

        Session("Operation") = "Update"

    End Sub

    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click

        UpdateAccounts(Session("Operation").ToString())

        rtvAccounts.DataBind()

        '''''' Show and Hide Controls ''''''

        nameEn.Visible = True
        nameAr.Visible = True
        Number.Visible = True
        Remarks.Visible = True
        Type.Visible = True

        rtbAccountNameEn.Visible = False
        rtbAccountNameAr.Visible = False
        rtbAccountNumber.Visible = False
        rtbAccountRemarks.Visible = False
        rcbAccountType.Visible = False

        btnEdit.Visible = True
        btnSave.Visible = False
        btnCancel.Visible = False

        pnlrtvAccounts.Visible = False

        '''''' Show and Hide Controls ''''''

    End Sub



    Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCancel.Click

        '''''' Show and Hide Controls ''''''

        nameEn.Visible = True
        nameAr.Visible = True
        Number.Visible = True
        Remarks.Visible = True
        Type.Visible = True

        lblParent.Visible = False
        lblParentAcc.Visible = False

        rtbAccountNameEn.Visible = False
        rtbAccountNameEn.Text = nameEn.Text
        rtbAccountNameAr.Visible = False
        rtbAccountNameAr.Text = nameAr.Text
        rtbAccountNumber.Visible = False
        rtbAccountNumber.Text = Number.Text
        rtbAccountRemarks.Visible = False
        rtbAccountRemarks.Text = Remarks.Text
        rcbAccountType.Visible = False
        'rcbAccountType.Text = Remarks.Text

        btnEdit.Visible = True
        btnSave.Visible = False
        btnCancel.Visible = False

        '''''' Show and Hide Controls ''''''

    End Sub



    Sub UpdateAccounts(ByVal Operation As String)

        Dim AccountObj As New AccountsSP
        Dim AccountDatatset As New Data.DataSet

        If Operation = "AddRoot" Then

            AccountDatatset = AccountObj.QueryDatabase(DBParentStoredProcedureClass.SqlQueryType.FillDataset, "saveAccount", rtbAccountNumber.Text, rtbAccountNameAr.Text, rtbAccountNameEn.Text, , rtbAccountRemarks.Text, rcbAccountType.SelectedValue)

        ElseIf Operation = "AddNew" Then

            AccountDatatset = AccountObj.QueryDatabase(DBParentStoredProcedureClass.SqlQueryType.FillDataset, "saveAccount", rtbAccountNumber.Text, rtbAccountNameAr.Text, rtbAccountNameEn.Text, lblNodeID.Text, rtbAccountRemarks.Text, rcbAccountType.SelectedValue)

        ElseIf Operation = "Update" Then

            AccountDatatset = AccountObj.QueryDatabase(DBParentStoredProcedureClass.SqlQueryType.FillDataset, "UpdateAccount", lblNodeID.Text, rtbAccountNameAr.Text, rtbAccountNameEn.Text, , rtbAccountRemarks.Text, rcbAccountType.SelectedValue)

        ElseIf Operation = "Delete" Then

            AccountDatatset = AccountObj.QueryDatabase(DBParentStoredProcedureClass.SqlQueryType.FillDataset, "deleteAccount", lblNodeID.Text, , , , )

        End If


    End Sub



End Class
